package com.example.ymm.modules1.service.impl.ms;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.dynamic.datasource.annotation.Slave;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.ms.TProviderPartner;
import com.example.ymm.modules1.entity.my.ms.MsMouldItem;
import com.example.ymm.modules1.entity.my.ms.MsMouldMain;
import com.example.ymm.modules1.mapper.ms.TProviderPartnerMapper;
import com.example.ymm.modules1.query.ms.TProviderPartnerQueryCriteria;
import com.example.ymm.modules1.service.dto.ms.DbMsOrderDto;
import com.example.ymm.modules1.service.dto.ms.MsOrderDto;
import com.example.ymm.modules1.service.dto.ms.MsOrderListQueryCriteria;
import com.example.ymm.modules1.service.ms.MsMouldItemService;
import com.example.ymm.modules1.service.ms.MsMouldMainService;
import com.example.ymm.modules1.service.ms.TProviderPartnerService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;

@Service
@DSTransactional
@Slf4j
@DS("slave")
public class TProviderPartnerServiceImpl extends BaseServiceImpl<TProviderPartnerMapper, TProviderPartner> implements TProviderPartnerService {


    @Autowired
    MsMouldMainService msMouldMainService;

    @Autowired
    MsMouldItemService msMouldItemService;

    @Override

    public TableList<TProviderPartner> list(TProviderPartnerQueryCriteria criteria, YmPageDto ymPage) {
        IPage<TProviderPartner> page = new Page<TProviderPartner>(ymPage.getPage(),ymPage.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(TProviderPartner.class, criteria);
        predicate.orderByDesc("oid");
        baseMapper.selectPage(page, predicate);
        TableList<TProviderPartner> tableList=new TableList(page);
        return tableList;
    }

    @Override
    public List<TProviderPartner> queryAll(TProviderPartnerQueryCriteria criteria) {
        List<TProviderPartner> result =baseMapper.selectList(QueryHelpPlus.getPredicate(TProviderPartner.class, criteria));
        return result;
    }

    @Override
    public TProviderPartner findLikeName(String name) {
        TProviderPartnerQueryCriteria criteria=new TProviderPartnerQueryCriteria();
        criteria.setKeyword(name);
        List<TProviderPartner> tProviderPartners = queryAll(criteria);
        TProviderPartner result=null;
        if(UtilValidate.isNotEmpty(tProviderPartners)){
            result=tProviderPartners.get(0);
        }
        return result;
    }

    @Override
    @DSTransactional
    public TableList<MsOrderDto> purchaseOrderTableList(MsOrderListQueryCriteria criteria, YmPageDto ymPage) {
//        getPage(ymPage);
        if(UtilValidate.isNotEmpty(criteria.getMaterialNoInsStr())){
            String[] split = criteria.getMaterialNoInsStr().split(",");
            List<String> list = Arrays.asList(split);
            criteria.setMaterialNoIn(list);
        }
        if(UtilValidate.isNotEmpty(criteria.getIsExcludeMain())){
           // 动态切换数据源
            DynamicDataSourceContextHolder.push("master");
            List<String> orderNos = msMouldMainService.getOrderNos(null);
            criteria.setOrderNoNotIn(orderNos);
            // 使用后清理，继续使用默认数据源
            DynamicDataSourceContextHolder.clear();
            DynamicDataSourceContextHolder.push("slave");
        }

        getPage(ymPage);
//        PageHelper.offsetPage(ymPage.getPage(),ymPage.getPageSize());
        List<MsOrderDto> maps = baseMapper.purchaseOrderList(criteria);
        PageInfo<MsOrderDto> pageInfo = new PageInfo<>(maps);
        TableList<MsOrderDto> tableList=new TableList<>(pageInfo);
        return tableList;
    }

    @Override
    @DSTransactional
    public TableList<MsOrderDto> priceTableList(MsOrderListQueryCriteria criteria, YmPageDto ymPage) {
        criteria.setIfXuNiJian(1);
        getPage(ymPage);
        List<MsOrderDto> maps = baseMapper.purchaseOrderList(criteria);
        PageInfo<MsOrderDto> pageInfo = new PageInfo<>(maps);
        TableList<MsOrderDto> tableList=new TableList<>(pageInfo);
        return tableList;
    }

    @Override
    public TableList<DbMsOrderDto> getPriceTableList(MsOrderListQueryCriteria criteria, YmPageDto ymPage) {
        TableList<MsOrderDto> msOrderDtoTableList = priceTableList(criteria, ymPage);
        List<DbMsOrderDto> list=new ArrayList<>();
        // 动态切换数据源
        DynamicDataSourceContextHolder.push("master");
        for(MsOrderDto dto: msOrderDtoTableList.getList()){
            DbMsOrderDto dbMsOrderDto=new DbMsOrderDto();
            BeanUtils.copyProperties(dto,dbMsOrderDto);


            //查询物料绑定
            MsMouldMain main = msMouldMainService.findOrderNo(dbMsOrderDto.getOrderNo());
            DynamicDataSourceContextHolder.clear();

            if(UtilValidate.isNotEmpty(main)){
                dbMsOrderDto.setMouldMainId(main.getMouldMainId());
                dbMsOrderDto.setReturnConditionType(main.getReturnConditionType());
                dbMsOrderDto.setReturnConditionNum(main.getReturnConditionNum());
                dbMsOrderDto.setStartDate(main.getStartDate());
                List<MsMouldItem> items = msMouldItemService.getItems(main.getMouldMainId());
                main.setItems(items);
                Integer isReturn = msMouldMainService.getIsReturn(main, 0);
                dbMsOrderDto.setIsReturn(isReturn);
            }


            list.add(dbMsOrderDto);
        }
        TableList<DbMsOrderDto> tableList=new TableList<>(msOrderDtoTableList);

        // 使用后清理，继续使用默认数据源
        tableList.setList(list);
        return tableList;
    }

    @Override
    public TableList<MsOrderDto> outsourceOrderTableList(MsOrderListQueryCriteria criteria, YmPageDto ymPage) {
        if(UtilValidate.isNotEmpty(criteria.getMaterialNoInsStr())){
            String[] split = criteria.getMaterialNoInsStr().split(",");
            List<String> list = Arrays.asList(split);
            criteria.setMaterialNoIn(list);
        }
        getPage(ymPage);
        List<MsOrderDto> maps = baseMapper.outsourceOrderList(criteria);
        PageInfo<MsOrderDto> pageInfo = new PageInfo<>(maps);
        TableList<MsOrderDto> tableList=new TableList<>(pageInfo);
        return tableList;
    }

    @Override
    public List<MsOrderDto> purchaseOrderList(MsOrderListQueryCriteria criteria) {
        return baseMapper.purchaseOrderList(criteria);
    }

    @Override
    public List<MsOrderDto> outsourceOrderList(MsOrderListQueryCriteria criteria) {
        return baseMapper.outsourceOrderList(criteria);
    }

    @Override
    public Double purchaseOrderSum(MsOrderListQueryCriteria criteria) {
        return baseMapper.purchaseOrderSum(criteria);
    }

    @Override
    public Double outsourceOrderSum(MsOrderListQueryCriteria criteria) {
        return baseMapper.outsourceOrderSum(criteria);
    }

    @Override
    public void testFn() {
        YmPageDto ymPage=new YmPageDto();
        ymPage.setPage(1);
        ymPage.setPageSize(10);
        MsOrderListQueryCriteria criteria=new MsOrderListQueryCriteria();
        criteria.setOrderDateStart("2024-09-04");
        criteria.setSupplierId("2");
        List<String> materialNoIn=new ArrayList<>();
        materialNoIn.add("05170002");
//        criteria.setOrderNumMax(1212);
//        TableList<MsOrderDto> purchaseTable = purchaseOrderTableList(criteria, ymPage);
//        TableList<MsOrderDto> outsourceTable = outsourceOrderTableList(criteria, ymPage);
//
//        System.out.println(outsourceTable);

        Double aDouble = outsourceOrderSum(criteria);
        System.out.println(aDouble);

    }
}
